# Copyright (C) 2015-2016  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.


cmake_minimum_required(VERSION 2.8.9)


set(PROJECT_NAME "PlanetWars2dRT-SDL2")
if (CMAKE_VERSION VERSION_LESS "3.1")
  project("${PROJECT_NAME}")
  # With language parameter as "C", CMake would have forced C90!
else()
  project("${PROJECT_NAME}" C)
endif()


option(BUILD_SHARED_LIB "Build shared library"    ON)
option(BUILD_STATIC_LIB "Build static library"    ON)
option(BUILD_SHARED_EXE "Build shared executable" ON)
option(BUILD_STATIC_EXE "Build static executable" ON)
option(BUILD_UNIT_TESTS "Build unit tests"        ON)

if(BUILD_SHARED_EXE)
  set(BUILD_SHARED_LIB ON)
endif()
if(BUILD_STATIC_EXE)
  set(BUILD_STATIC_LIB ON)
endif()


# Configuration of compilators

## Compiler flags to add
set(GNU_FLAGS_WARNING  "-Wall -Wextra -Wformat=2 -Wpedantic -Werror")
set(GNU_FLAGS_OPTIMIZE "-O2")
set(GNU_FLAGS_SECURITY "-fPIC -D_FORTIFY_SOURCE=2")
set(GNU_FLAGS_DEBUG    "-O0 -g")
set(GNU_C_FLAGS
  "${GNU_FLAGS_WARNING} ${GNU_FLAGS_OPTIMIZE} ${GNU_FLAGS_SECURITY}")
set(GNU_C_FLAGS_DEBUG
  "${GNU_FLAGS_WARNING} ${GNU_FLAGS_DEBUG}")

## C compiler options
if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR
    "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
  set(CMAKE_C_FLAGS       "${CMAKE_C_FLAGS}       ${GNU_C_FLAGS}")
  set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${GNU_C_FLAGS_DEBUG}")
endif()

### C99
if (CMAKE_VERSION VERSION_LESS "3.1")
  if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR
      CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
    set(CMAKE_C_FLAGS       "${CMAKE_C_FLAGS}       -std=c99")
    set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -std=c99")
  endif()
else()
  set(CMAKE_C_STANDARD 99)
endif()


# Version number
set(VERSION_MAJOR "0")
set(VERSION_MINOR "0")
set(VERSION_MICRO "0")

# Configure a header file to pass some of the CMake settings
# to the source code.
configure_file(
  "src/specific/compilation_config.h.in"
  "${PROJECT_BINARY_DIR}/include/PlanetWars2dRT-SDL2/specific/compilation_config.h"
  )
# Add the binary tree to the search path for include files
# so that we will find compilation_config.h
include_directories(${PROJECT_BINARY_DIR}/include/)


set(LIBRARY_OUTPUT_PATH    lib/)
set(EXECUTABLE_OUTPUT_PATH bin/)

include_directories(
  ${PROJECT_SOURCE_DIR}/include/
  ${PROJECT_BINARY_DIR}/include/
  )
file(
  GLOB_RECURSE
  lib_source_files
  src/*
  )
set(exe_source_files src/specific/view/planet-wars-2d-rt-sdl2-gui.c)
list(REMOVE_ITEM lib_source_files ${exe_source_files})

if(BUILD_SHARED_LIB)
  add_library(planet-wars-2d-rt-sdl2        SHARED ${lib_source_files})
endif()
if(BUILD_STATIC_LIB)
  add_library(planet-wars-2d-rt-sdl2_static STATIC ${lib_source_files})
endif()


# Dependency checking
include(FindPkgConfig)

pkg_search_module(PLANET_WARS_2D_RT_CORE OPTIONAL PlanetWars2dRT-core)
if(PLANET_WARS_2D_RT_CORE_FOUND)
  message("Using shared PlanetWars2dRT-core")
  include_directories(${PLANET_WARS_2D_RT_CORE_INCLUDE_DIRS})
endif()
if(NOT PLANET_WARS_2D_RT_CORE_FOUND OR BUILD_STATIC_EXE)
  message("Using static PlanetWars2dRT-core from externals")
  add_subdirectory(externals/core)
  include_directories(
    externals/core/include/
    externals/core/build/include/
    )
endif()

pkg_search_module(SDL2 OPTIONAL sdl2)
if(SDL2_FOUND)
  message("Using shared SDL2")
  include_directories(${SDL2_INCLUDE_DIRS})
else()
  message("Using static SDL2 from externals")
  # TODO compile SDL2 staticly with CMake
  include_directories(externals/SDL2/include/)
  link_directories(${CMAKE_SOURCE_DIR}/externals/SDL2/build/)
  set(SDL2_LIBRARIES libSDL2.a dl pthread)
endif()

pkg_search_module(SDL2GFX OPTIONAL SDL2_gfx)
if(SDL2GFX_FOUND)
  message("Using shared SDL2_gfx")
  include_directories(${SDL2GFX_INCLUDE_DIRS})
else()
  message("Using static SDL2_gfx from externals")
  # TODO compile SDL2_gfx staticly with CMake
  include_directories(externals/SDL2_gfx/)
  link_directories(${CMAKE_SOURCE_DIR}/externals/SDL2_gfx/.libs/)
  set(SDL2GFX_LIBRARIES libSDL2_gfx.a)
endif()

pkg_search_module(SDL2IMG OPTIONAL SDL2_image)
if(SDL2IMG_FOUND)
  message("Using shared SDL2_image")
  include_directories(${SDL2IMG_INCLUDE_DIRS})
else()
  message("Using static SDL2_image from externals")
  # TODO compile SDL2_image staticly with CMake
  include_directories(externals/SDL2_image/)
  link_directories(${CMAKE_SOURCE_DIR}/externals/SDL2_image/.libs/)
  set(SDL2IMG_LIBRARIES libSDL2_image.a)
endif()


# Executable

if(BUILD_SHARED_EXE)
  add_executable(planet-wars-2d-rt-sdl2-gui ${exe_source_files})
  target_link_libraries(
    planet-wars-2d-rt-sdl2-gui
    planet-wars-2d-rt-sdl2
    planet-wars-2d-rt-core
    m # math
    ${SDL2_LIBRARIES}
    ${SDL2GFX_LIBRARIES}
    ${SDL2IMG_LIBRARIES}
    )
  install(TARGETS planet-wars-2d-rt-sdl2-gui DESTINATION bin)
endif()

if(BUILD_STATIC_EXE)
  add_executable(planet-wars-2d-rt-sdl2-gui_static ${exe_source_files})
  target_link_libraries(
    planet-wars-2d-rt-sdl2-gui_static
    planet-wars-2d-rt-sdl2_static
    planet-wars-2d-rt-core_static
    m # math
    ${SDL2_LIBRARIES}
    ${SDL2GFX_LIBRARIES}
    ${SDL2IMG_LIBRARIES}
    )
endif()


# Package

set(CPACK_PACKAGE_VERSION_MAJOR ${VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${VERSION_MINOR})
set(CPACK_PACKAGE_VERSION_PATCH ${VERSION_MICRO})

set(CPACK_GENERATOR "TBZ2;STGZ")
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
  set(CPACK_GENERATOR "${CPACK_GENERATOR};DEB;RPM")
endif()

set(CPACK_PACKAGE_DESCRIPTION_SUMMARY
  "A free/libre real-time 2D planet wars game with SDL2")
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE.md")
set(CPACK_RESOURCE_FILE_README  "${CMAKE_CURRENT_SOURCE_DIR}/README.md")

set(CPACK_DEBIAN_PACKAGE_DEPENDS
  "libc6, libsdl2-2.0-0, libsdl2-gfx-1.0-0, libsdl2-image-2.0-0")
set(CPACK_DEBIAN_PACKAGE_SECTION "games")
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Nicola Spanti")

include(CPack)
